from django.http import HttpResponse
from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.template import Context, loader

#from registration.forms import RegistrationForm
from django.contrib.auth.models import User
from best_buy_book.shop.models import Autor
from django.forms.models import modelformset_factory

def nuevoAutor(request):
    
    AutorFormSet = modelformset_factory(Autor)
#                                        exclude=['groups',
#                                                            'is_staff',
#                                                            'is_superuser',
#                                                            'is_active',
#                                                            'user_permissions',
#                                                            ], max_num='1', extra='0')
    
    data = {
        'form-TOTAL_FORMS': u'1',
        'form-INITIAL_FORMS':u'0',
        'form-0-username': u'nombre_prueba',
        'form-0-first_name': u'nombre_prueba',
        'form-0-last_name': u'nombre_prueba',
        'form-0-biografia': u'biography',
        'form-0-password':u'passy',
        'form-0-email':u'one.victor@gmail.com',
        'form-0-pais': u'nombre_prueba',
        'form-0-fecha_nacimiento':u'0001-01-01',
        'form-0-defuncion': u'0001-01-01',
        'form-0-last_login':'0001-01-01',
        'form-0-date_joined':'0001-01-01',
        
    }
    
    
    formset = AutorFormSet(data)#bound
    #return HttpResponse('hi there :D')
    return render_to_response('registration/nuevoAutor.html',
                             { 'formset': formset})
    
  

def complete(request):
    autorformset = modelformset_factory(Autor)
    if request.method == 'POST': # If the form has been submitted...
        formset = autorformset(data=request.POST) # A form bound to the POST data
        if formset.is_valid(): # All validation rules pass
            newAutor = Autor(username=request.POST['form-0-username'], first_name=request.POST['form-0-first_name'],
                               last_name=request.POST['form-0-last_name'], password=request.POST['form-0-password'],
                               email=request.POST['form-0-email'], fecha_nacimiento = request.POST['form-0-fecha_nacimiento'],
                               is_active=False, defuncion = request.POST['form-0-defuncion'], biografia = request.POST['form-0-defuncion'],
                               pais = request.POST['form-0-pais'])
            newAutor.save()

            return HttpResponse('todo bien!')
            #return render_to_response('registration/registration_complete.html',
            #                 {})
        else:
       
            return HttpResponse(formset.errors)
        
        #  return HttpResponseRedirect('/accounts/register/', {'errores': formset.errors})
    else:
        formset = clienteformset() # An unbound form
        return HttpResponseRedirect('/accounts/manager/nuevoAutor/')
